package com.lnkj.spcj.ui.camera

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.SystemClock
import android.view.View
import android.view.WindowManager
import com.lnkj.spcj.R
import com.lnkj.spcj.base.BaseActivity2
import com.lnkj.spcj.ui.scan.ScanResultBean
import com.lnkj.spcj.ui.upload.UploadActivity
import com.lnkj.spcj.util.utilcode.LogUtils
import com.lnkj.spcj.util.utilcode.ToastUtils
import com.wonderkiln.camerakit.*
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_camera.*
import kotlinx.android.synthetic.main.layout_topbar.*
import org.jetbrains.anko.startActivity
import java.util.concurrent.TimeUnit

class CameraActivity : BaseActivity2() {
    override val layoutRes: Int
        get() = R.layout.activity_camera
    var bean: ScanResultBean? = null

    override fun initAll() {
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        tv_title.text = "视频录制"
        bean = intent.getSerializableExtra("bean") as ScanResultBean
        cameraView.setVideoQuality(CameraKit.Constants.VIDEO_QUALITY_720P)
        cameraView.setVideoBitRate(2000000)
        val filter = IntentFilter()
        filter.addAction("android.intent.action.PRESS_SOS_KEY")
        filter.addAction("android.intent.action.PRESS_VIDEO_KEY")
        filter.addAction("android.intent.action.PRESS_RECORD_KEY")
        filter.addAction("android.intent.action.PRESS_MARK_KEY")
        filter.addAction("android.intent.action.PRESS_PTT_KEY")
        filter.addAction("android.intent.action.PRESS_TUCHUAN_KEY")
        filter.addAction("android.intent.action..PRESS_PIC_KEY")
        registerReceiver(broadcastReceiver,filter)
    }


   var broadcastReceiver=object: BroadcastReceiver(){
       override fun onReceive(p0: Context?, p1: Intent?) {
           btn_start.performClick()
       }
   }

    override fun setListener() {
        iv_left.setOnClickListener {
            onBackPressed()
        }
        cameraView.addCameraKitListener(object : CameraKitEventListener {
            override fun onVideo(p0: CameraKitVideo?) {
                timer.stop()
                LogUtils.e(TAG, p0?.videoFile?.toString())
                startActivity<UploadActivity>("bean" to bean, "path" to p0?.videoFile?.toString())
                finish()
            }

            override fun onEvent(p0: CameraKitEvent?) {

            }

            override fun onImage(p0: CameraKitImage?) {

            }

            override fun onError(p0: CameraKitError?) {
                ToastUtils.showShort(p0?.message)
            }
        })
        iv_switch.setOnClickListener {
            cameraView.toggleFacing()
        }
        btn_start.setOnClickListener {
            when (btn_start.text.toString()) {
                "开始录制" -> {
                    cameraView.captureVideo()
                    timer.base= SystemClock.elapsedRealtime()
                    timer.start()
                    btn_start.text = "结束录制"
                    iv_switch.visibility = View.GONE
                }
                "结束录制" -> {
                    cameraView.stopVideo()
                    btn_start.text = "上传视频"
                }
            }
        }
    }

    var timerRx:Disposable?=null
    override fun processLogic() {
        timerRx = Observable.timer(500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe {
             btn_start.performClick()
        }
    }

    override fun onResume() {
        super.onResume()
        cameraView.start()
    }

    override fun onPause() {
//        cameraView.stop()
        super.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        cameraView.stop()
        timerRx?.dispose()
        unregisterReceiver(broadcastReceiver)
    }
}